#ifndef MUSICPLAYER_H
#define MUSICPLAYER_H

#include <QWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPropertyAnimation>
#include <QSqlDatabase>
#include "volumetool.h"
#include "musiclist.h"
#include "commonpage.h"
#include "lrcpage.h"

const qreal speed = 1.25;
QT_BEGIN_NAMESPACE
namespace Ui { class MusicPlayer; }
QT_END_NAMESPACE

class MusicPlayer : public QWidget
{
    Q_OBJECT

public:
    MusicPlayer(QWidget *parent = nullptr);
    ~MusicPlayer();

    void initUi();
    void connectSignalAndSlot();
    // 初始化推荐图片路径
    QJsonArray randomPicture();
    // 初始化播放媒体
    void initPlayer();
    // 初始化数据库
    void initSQLite();
    // 初始化歌单
    void initMusicList();
    // 显示播放动画
    void updateBtformAnimation();
protected:
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
private slots:
    void on_quit_clicked();
    void onBtFormClick(int pageId);
    void on_volume_clicked();

    void on_addLocal_clicked();
    void onUpdateLikeMusic(bool isLike, QString musicId);
    void on_Play_clicked();
    // 切换播放按钮图标
    void onPlayStateChanged();
    void on_playUp_clicked();

    void on_playDown_clicked();

    void on_playMode_clicked();
    void onPlaybackModeChanged(QMediaPlaylist::PlaybackMode playbackMode);
    // 初始化当前页面
    void onPlayAll(PageType pageType);
    // 加载当前页面所有歌曲并播放
    void playAllOfCommonPage(CommonPage* commonPage, int index);
    // 双击播放
    void playMusicByIndex(CommonPage *page, int index);
    // 当前播放内容改变，添加历史记录
    void onCurrentIndexChanged(int index);
    // 静音
    void setMusicSilence(bool isMuted);
    // 设置音量
    void setPlayerVolume(int volume);
    // 总时间变化
    void onDurationChanged(qint64 duration);
    // 当前时间变化
    void onPositionChanged(qint64 duration);
    // 进度条设置播放进度
    void onMusicSliderChanged(float value);
    // 媒体源改变
    void onMetaDataAvailableChanged(bool available);
    void on_lrcWord_clicked();

    void on_pushButton_clicked();

    void on_skin_clicked();

    void on_min_clicked();
    void quitMusicPlayer();
private:
    Ui::MusicPlayer *ui;
    QPoint dragPosition;
    VolumeTool* volumeTool;
    MusicList musicList;
    QMediaPlayer* player;
    QMediaPlaylist* playList;
    CommonPage* currentPage;
    qint64 totalTime;
    int currentIndex;
    LrcPage* lrcPage;
    QPropertyAnimation* lrcAnimation;
    bool isSpeed;
    QSqlDatabase sqlite;
    bool isDrag;
};
#endif // MUSICPLAYER_H
